//
//  FileEntity.m
//  TcpTest_Client
//
//  Created by scarlettsun on 12-8-1.
//  Copyright (c) 2012年 tencent. All rights reserved.
//

#import "FileEntity.h"
#import "BuddyGroup.h"
#import "MainWindowController.h"

@implementation FileEntity

@synthesize senderId;
@synthesize receiverId;
@synthesize fileUrl;
@synthesize fileSize;
@synthesize fileSentSize;
@synthesize fileReceiveSize;
@synthesize fileHandler;
@synthesize photo;
@synthesize fileState;
@synthesize delegate;


-(void) dealloc
{
    self.fileHandler = nil;
    self.fileUrl = nil;
}

-(FileEntity *) initWithFilePath:(NSURL *)_filePath sender:(Buddy *)myInfo 
                        receiver:(Buddy *)peerInfo fileSize:(NSUInteger)_fileSize;
{
    self.fileUrl = _filePath;
    self.fileHandler = [NSFileHandle fileHandleForUpdatingAtPath:[_filePath path]];
    self.senderId = myInfo.buddyQQ;
    self.receiverId = peerInfo.buddyQQ;
    self.fileSize = _fileSize;
    self.fileSentSize = 0;
    self.fileReceiveSize = 0;
    self.fileState = EntityWaiting;
    self.photo = peerInfo.photo;
    return self;
}

-(FileEntity *) initWithFilePath:(NSURL *)_filePath senderId:(NSUInteger )_senderId
                        receiver:(Buddy *)myInfo fileSize:(NSUInteger)_fileSize;
{
    self.fileUrl = _filePath;
    self.fileHandler = [NSFileHandle fileHandleForUpdatingAtPath:[_filePath path]];
    self.senderId = _senderId;
    self.receiverId = myInfo.buddyQQ;
    self.fileSize = _fileSize;
    self.fileSentSize = 0;
    self.fileReceiveSize = 0;
    self.fileState = EntityWaiting;
    self.photo = [self getPeerPhoto:_senderId];
    return self;
}


-(NSString *) getPeerPhoto:(NSUInteger)peerId
{
    for (BuddyGroup *group in ((MainWindowController *)[MainWindowController sharedMainWindowController]).buddySource.buddyGroupArray)
    {
        for (Buddy* buddy in group->buddyList) 
        {
            if (buddy.buddyQQ == peerId) 
            {
                return buddy.photo;
            }
        }
    }
    return nil;
}

@end
